import vue from "@vitejs/plugin-vue";
import path from "path";
import { defineConfig } from "vite";
import electron from "vite-plugin-electron";
import { svgBuilder } from "./src/utils/svgBuilder";
import { viteCommonjs } from "@originjs/vite-plugin-commonjs";
import {
  themePreprocessorPlugin,
  themePreprocessorHmrPlugin
} from '@zougt/vite-plugin-theme-preprocessor' //主题切换中间件
//优化vite首次加载缓慢
import OptimizationPersist from 'vite-plugin-optimize-persist'
import PkgConfig from 'vite-plugin-package-config'

const resolvePath = (dir: string) => path.join(__dirname, dir);

// https://vitejs.dev/config/
export default defineConfig({
	optimizeDeps: {
		exclude: ["electron","@zougt/vite-plugin-theme-preprocessor/dist/browser-utils"] // 告诉 Vite 排除预构建 electron , browser-utils.js，不然会出现 __diranme is not defined
	},
	css: {
		preprocessorOptions: {
			less: {
				modifyVars: {
					"primary-color": "#000",
					"link-color": "red"
				},
				javascriptEnabled: true,
				additionalData: `@import "${resolvePath("./src/theme/dark.less")}";`
			}
		},
		postcss: {
			plugins: []
		}
	},
	plugins: [
		PkgConfig(),
        OptimizationPersist(),
		vue(),
		themePreprocessorPlugin({
         less: {
        // 各个主题文件的位置
         multipleScopeVars: [
            {
              scopeName: 'theme-dark',
              path: path.resolve('src/theme/dark.less')
            },
            {
              scopeName: 'theme-green',
              path: path.resolve('src/theme/green.less')
            },
            {
              scopeName: 'theme-red',
              path: path.resolve('src/theme/red.less')
            },
            {
              scopeName: 'theme-brown',
              path: path.resolve('src/theme/brown.less')
            }
          ],
		defaultScopeName: "theme-dark", // 默认取 multipleScopeVars[0].scopeName
        extract: false,// 在生产模式是否抽取独立的主题css文件
        }
      }),
      themePreprocessorHmrPlugin(), // 主题热更新，不得已分开插件，因为需要vite插件顺序enforce  注意：出现多个主题需要注释当前，可以防止乱序打印主题不生效
		svgBuilder(resolvePath("./src/assets/svg/")),
		viteCommonjs(),
		electron({
			main: {
				entry: "electron/main/index.ts",
				vite: {
					build: {
						outDir: "dist/electron/main"
					}
				}
			},
			preload: {
				input: {
					// Must be use absolute path, this is the restrict of Rollup
					index: resolvePath("electron/preload/index.ts")
				},
				vite: {
					build: {
						outDir: "dist/electron/preload"
					}
				}
			}
		}),
			
	],
	
	define: {
		"process.env": {
			BASE_API: "http://192.168.31.230:8082",
			BASE_URL: "http://192.168.31.119:8084",
			API_WW: "http://118.24.254.66:8083",
			BASE_118: "http://118.24.254.66:8081",
			API_HTTP: "http://118.24.254.66:8083"
		}
	},
	base: "./",
	
	server: {
		port: 3000, //启动端口
		hmr: {
			overlay: true, // 禁用或配置 HMR 连接 设置 server.hmr.overlay 为 false 可以禁用服务器错误遮罩层
			host: "127.0.0.1",
			port: 3000
		},
		open: false, // 类型： boolean | string在服务器启动时自动在浏览器中打开应用程序；
		cors: true, // 类型： boolean | CorsOptions 为开发服务器配置 CORS。默认启用并允许任何源
		// 设置 https 代理
		proxy: {
			"/api": {
				target: process.env.API_HTTP,
				changeOrigin: true,
				rewrite: (path: string) => path.replace(/^\/api/, "")
			}
		}
	},
	resolve: {
		extensions: [".ts", ".tsx", ".js", ".json"],
		alias: {
			"@": resolvePath("./src")
		}
	}
});
